using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using SubsidiosBussinessLogic.DataAccess.Context;
using SubsidiosBussinessLogic.DataAccess.Interfaces;
using SubsidiosBussinessLogic.DataAccess.SqlServerRepository;
using SubsidiosWebMvc.CommonData;

namespace SubsidiosWebMvc.Controllers
{
    public class ExpedienteController : Controller
    {
        
        private IExpedienteRepository  Repository { get; set; }
        
        #region .ctor
        public ExpedienteController() : this (new ExpedienteRepository(DataContextFactory.GetContext()))
        {}

        public ExpedienteController(IExpedienteRepository repository)
        {
            Repository = repository;
        }
        #endregion
        //
        // GET: /Expediente/
        
        public ActionResult Listar()
        {
            return View();
        }
        #region para borrar despues
        //
        // GET: /Expediente/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Expediente/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Expediente/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Expediente/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Expediente/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        #endregion

        public ActionResult GridData(string sidx, string sord, int page, int rows, bool _search, string searchField, string searchOper, string searchString)
        {
            var expedientes = Repository.GetExpedientes(); 
            var jsonData = new JsonData { page = page, records = 3, rows = new List<Rows>() };
            var pageIndex = page - 1;
            jsonData.page = page;
            jsonData.records = expedientes.Count();
            jsonData.total = (int)Math.Ceiling((float)jsonData.records / (float)rows);
            jsonData.rows = new List<Rows>();

            foreach (var expediente in expedientes)
            {

                jsonData.rows.Add(new Rows { id = expediente.ExpedienteId, cell = new string[]
                                                                                      {
                                                                                          expediente.ExpedienteId.ToString(), 
                                                                                          expediente.FuncionarioId.ToString(), 
                                                                                          expediente.NroExpediente,
                                                                                          expediente.FechaCaratulacion.ToString(),
                                                                                          expediente.TipoEstadoExpedienteId.ToString()
                                                                                      } });

            }

            return Json(jsonData);

        }


    }
}
